package com.authine.cloudpivot.ext.domain.vo;

import com.authine.cloudpivot.ext.dto.goods.VendorGoodsEntity;
import com.authine.cloudpivot.ext.utils.kingdee.DateUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Range;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by Ezreal_Guo on 2019/4/26.
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("供应商的商品物料")
public class VendorGoodsVO {
    //商品id
    @ApiModelProperty(value = "商品id")
    private String goodsId;
    @ApiModelProperty(value = "品牌")
    private String brand;
    @ApiModelProperty("名称")
    private String name;
    @ApiModelProperty(value = "型号")
    private String model;
    @ApiModelProperty(value = "规格")
    private String spec;
    @ApiModelProperty(value = "类目")
    private String category;
    @ApiModelProperty(value = "销售单位", example = "个")
    private String unit;
    @ApiModelProperty(value = "包装内数量")
    private Double packageNum;
    @ApiModelProperty(value = "包装单位")
    private String packageUnit;
    @ApiModelProperty(value = "最小起订量", dataType = "double", example = "30.0")
    private Double minOrder;
    @ApiModelProperty(value = "税率", dataType = "double", example = "0.13")
    private Double taxRate;
    @ApiModelProperty(value = "未税单价")
    @Range(max = 2, message = "请填写正确单价")
    private Double pretaxPrice;
    @ApiModelProperty(value = "含税单价")
    private Double price;
    @ApiModelProperty(value = "库存数量")
    private Double stock;
    @ApiModelProperty(value = "产地")
    private String place;
    @ApiModelProperty(value = "发货地", dataType = "string", example = "上海")
    private String deliveryPlace;
    @ApiModelProperty(value = "无库存交货期(天)", dataType = "integer", example = "1")
    //商城新增 start
    private Integer delivery;
    @ApiModelProperty(value = "现货货期(天)", dataType = "integer", example = "1")
    private Integer stockDelivery;
    @ApiModelProperty(value = "安全库存", dataType = "integer", example = "5000")
    //商城新增 end
    private Integer safeStock;
    @ApiModelProperty(value = "保修期", dataType = "string", example = "1年")
    private String warranty;
    @ApiModelProperty(value = "sku编码")
    private String sku;
    @ApiModelProperty(value = "条形码")
    private String barCode;
    @ApiModelProperty(value = "金税编码")
    private String goldenTax;
    @ApiModelProperty(value = "美国电子目录编码")
    private String unspsc;
    @ApiModelProperty(value = "商品描述")
    private String description;
    //    @ApiModelProperty(value="运费比例", dataType = "string", example = "0.04")
//    private Double freightRatio;
    @ApiModelProperty(value = "技术参数")
    private String technical;
    @ApiModelProperty(value = "包装参数")
    private String packaging;
    @ApiModelProperty(value = "销售有效期", dataType = "date", example = "2020-12-20 00:00:00")
    @JsonFormat(pattern = DateUtils.FORMAT_PATTERN_DATETIME)
    private Date expiry;
    @ApiModelProperty(value = "长")
    private Double length;
    @ApiModelProperty(value = "宽")
    private Double width;
    @ApiModelProperty(value = "高")
    private Double height;
    @ApiModelProperty(value = "重量")
    private Double weight;
    @ApiModelProperty(value = "商品备注")
    private String memo;
    @ApiModelProperty(value = "是否启用", notes = "被禁用的商品无法上架销售")
    private Boolean enabled;
    @ApiModelProperty(value = "是否上架")
    private Boolean saled;
    @ApiModelProperty(value = "货期要求")
    private String deliveryClaim;
    //图片
    @ApiModelProperty(value = "图片", notes = "多张图片以逗号(,)分隔")
    private String images;
    @ApiModelProperty(value = "供应商名称")
    private String orgFullname;
    @ApiModelProperty(value = "上传时间", example = "2019-12-20 00:00:00")
    @JsonFormat(pattern = DateUtils.FORMAT_PATTERN_DATETIME)
    private Date time;
    @ApiModelProperty(value = "类别")
    private String catalog;
    @ApiModelProperty(value = "外部来源客户端id")
    private String clientId;
    @ApiModelProperty(value = "外部来源商品id", notes = "同组织下不可重复")
    private String clientGoodsId;
    @ApiModelProperty(hidden = true)
    private String vendorGoodsId; //核心品牌库新增(暂未使用)
    @ApiModelProperty(hidden = true)
    private Integer importLine; //用于文件解析
    @ApiModelProperty(hidden = true)
    private Boolean core;


    public static VendorGoodsEntity vendorGoodsVoToGoodsEntity(VendorGoodsVO vgv) {
        VendorGoodsEntity.VendorGoodsEntityBuilder builder = VendorGoodsEntity.builder();

        builder.id(vgv.getGoodsId())
                .catalog(vgv.getCatalog())
                .unit(vgv.getUnit())
                .package_unit(vgv.getPackageUnit())
                .package_num(convertBigDecimal(vgv.getPackageNum()))
                .pretax_price(convertBigDecimal(vgv.getPretaxPrice()))
                .price(convertBigDecimal(vgv.getPrice()))
                .tax_rate(convertBigDecimal(vgv.getPrice()))
                .sku(vgv.getSku())
                .stock(convertBigDecimal(vgv.getStock()))
                .warranty(vgv.getWarranty())
                .delivery(vgv.getDelivery() == null ? null : String.valueOf(vgv.getDelivery()))
                .min_order(convertBigDecimal(vgv.getMinOrder()))
                .expiry(vgv.getExpiry())
                .length(convertBigDecimal(vgv.getLength()))
                .width(convertBigDecimal(vgv.getWidth()))
                .height(convertBigDecimal(vgv.getHeight()))
                .weight(convertBigDecimal(vgv.getWeight()))
                .saled(vgv.getSaled())
                .enabled(vgv.getEnabled())
                .delivery_claim(vgv.getDeliveryClaim())
                .org_fullname(vgv.getOrgFullname())
                .client_id(vgv.getClientId())
                .client_goods_id(vgv.getClientGoodsId())
                .safe_stock(vgv.getSafeStock() == null ? null : String.valueOf(vgv.getSafeStock()))
                .stock_delivery(vgv.getStockDelivery() == null ? null : String.valueOf(vgv.getStockDelivery()))
                .category(vgv.getCategory())
                .brand(vgv.getBrand())
                .name(vgv.getName())
                .model(vgv.getModel())
                .spec(vgv.getSpec())
                .bar_code(vgv.getBarCode())
                .golden_tax(vgv.getGoldenTax())
                .unspsc(vgv.getUnspsc())
                .place(vgv.getPlace())
//                .images(vgv.images)//暂时忽略,等最终方案
                .description(vgv.getDescription())
                .technical(vgv.getTechnical())
                .packaging(vgv.getPackaging())
                .memo(vgv.getMemo())
                .delivery_place(vgv.getDeliveryPlace());
        return builder.build();
    }

    private static BigDecimal convertBigDecimal(Object number) {

        return number == null ? null : new BigDecimal(String.valueOf(number));
    }

}
